package cruds.accessories.brand;

import items.BrandDTO;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;

import cruds.CrudCreateParent;
import dynamicmenus.BrandModelMenu;
import exceptions.EmptyFieldException;
import home.Home;
import services.BrandDTOService;
import utils.StaticMethods;


@SuppressWarnings("serial")
public class CrudCreateBrandDTO extends CrudCreateParent<BrandDTO>{

	private String type;
	
	public CrudCreateBrandDTO(Home home,String type) {
		super(home);
		this.type = type;
	}

	@Override
	protected void initializeService() {
		BrandDTOService service = this.getHome().getGeneralService().getBrandSer();
		this.setService(service);
	}

	@Override
	protected void redirectToPageIfCancel(AjaxRequestTarget target) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void updateDynamicMenu(AjaxRequestTarget target) {
		BrandModelMenu menu = new BrandModelMenu(this.getHome());
		menu.changePanel(target);
	}

	@Override
	protected void doSaveAndRedirect(AjaxRequestTarget target) {
		try{
			StaticMethods.hasEmptyFields(this.getItem());
			this.getItem().setCategory(this.type);
			this.getService().save(this.getItem());
			BrandsList panel = new BrandsList(this.getHome(),this.type,this.getHome().getGeneralService().getBrandSer().getBrandsAndModels(this.type));
			panel.changePanel(target);
		}catch(EmptyFieldException e){
			this.setErrorMessage(target, e.getMessage());
		}
	}

	@Override
	protected void createFormFields(Form<BrandDTO> form) {
		form.add(new TextField<String>("brand"));
		form.add(new TextField<String>("model"));
	}

	@Override
	protected void initializeEmptyItem() {
		this.setItem(new BrandDTO());
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
